import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-07-15
 * Time: 9:09
 */
public class ArrayList1 {
    public int[] arr;//数组
    public int size;//元素个数
    private final int capacity = 10;

    public ArrayList1() {
        arr = new int[capacity];
    }

    private void resize() {
        arr = Arrays.copyOf(arr, capacity * 2);
    }

    /**
     * 尾插
     *
     * @param val
     */
    public void add(int val) {
        if (size == capacity) {
            resize();
        }
        arr[size] = val;
        size++;
    }

    /**
     * 尾删
     */
    public int delete() {
        int ret = arr[size - 1];
        size--;
        return ret;
    }

    /**
     * 在指定位置插入
     */
    public void addIndex(int val, int index) {
        if (index < 0 || index > size) {
            return;
        }
        if (index == 0) {
            add(val);
        }
        if (index == size) {

        }
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < size; i++) {
            str.append(arr[i]);
            str.append(" ");
        }
        return str.toString();
    }
}
